﻿
<div style="height: 350px;overflow-y: auto;overflow-x: hidden;">
    <ul id="system-role-tree" class="ztree" style="padding: 20px 0px 20px 40px;"></ul>
</div>
<script>

    layui.use(['layer', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var admin = layui.admin;

        layer.load(2);
        var setting = {
            check: {enable: true,chkStyle: "checkbox"},
            data: {
                simpleData: {enable: true}
            }
        };
        admin.reqBackground('system/authority/list',{},function(json){
            if(json){
                var roleId = admin.getTempData("system-role-curr-roleId")
                if(roleId){
                    admin.reqBackground('system/role/authList',{roleId:roleId},function (data) {
                        initTree(json,data);
                    })
                }else{
                    initTree(json);
                }
            }
        })
        function initTree(json,authList){
            var data = [];
            for(var i=0;i<json.length;i++){
                var a = {};
                a.id = json[i].id;
                a.name = json[i].name;
                a.pId = json[i].parentId;
                a.open= true;
                if(json[i].authority){
                    a.name = json[i].name +" "+ json[i].authority;
                }
                if(authList && authList.length){
                    for (var j = 0;j<authList.length;j++){
                        if(authList[j].authId == a.id){
                            a.checked = true;
                            break;
                        }
                    }
                }
                data.push(a);
            }
            data.push({id:"-1",name:"根节点",open:true})
            $.fn.zTree.init($('#system-role-tree'), setting, data);
        }

        layer.closeAll('loading');

    });
</script>

</body>
</html>